#include "setupwidget.h"
#include "ui_setupwidget.h"
#include <QMessageBox>
#include <QComboBox>
#include <QStringList>
#include "adddevice.h"
#include "homepage.h"
setupWidget::setupWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setupWidget)
{
    ui->setupUi(this);
/*
    //给tabwidget设置图片
    QTabBar *tabBar = ui->tabWidget->tabBar();
    QIcon icon1(":/image/设置.png");  // 替换为你的图标路径
    QIcon icon2(":/image/库.png"); // 替换为你的图标路径
    tabBar->setTabIcon(0, icon1);
    tabBar->setTabIcon(1, icon2);
*/
    //完成表格填充
    initTable();
    //进行格式选择
   QFile file("::/new/prefix1/基础");

   file.open(QIODevice::ReadOnly);

   this->setStyleSheet( file.readAll() );

   file.close();
}

setupWidget::~setupWidget()
{
    delete ui;
}

void setupWidget::pushButtonClicked(int& num)
{
    if(num==0){
        num++;
        QIcon icon(":/new/prefix1/img/开启.png");
        btn->setIcon(icon);
    }else{
        num--;
        QIcon icon(":/new/prefix1/img/关闭.png");
        btn->setIcon(icon);
    }
}
//按钮
void setupWidget::on_pushButton_clicked()
{
    static int num=0;
    btn=ui->pushButton;
    pushButtonClicked(num);
}

void setupWidget::on_pushButton_2_clicked()
{
    static int num=0;
    btn=ui->pushButton_2;
    pushButtonClicked(num);
}


void setupWidget::on_pushButton_4_clicked()
{
    static int num=0;
    btn=ui->pushButton_4;
    pushButtonClicked(num);
}

void setupWidget::on_pushButton_3_clicked()
{
    static int num=0;
    btn=ui->pushButton_3;
    pushButtonClicked(num);

}

void setupWidget::on_pushButton_5_clicked()
{
    static int num=0;
    btn=ui->pushButton_5;
    pushButtonClicked(num);

}

void setupWidget::on_pushButton_6_clicked()
{
    static int num=0;
    btn=ui->pushButton_6;
    pushButtonClicked(num);

}
//样式选择
void setupWidget::on_comboBox_activated(const QString &arg1)
{
    QString filename = ":/new/prefix1/"+arg1;
    QFile file(filename);

    file.open(QIODevice::ReadOnly);

    HomePage homepage;

    homepage.setStyleSheet( file.readAll() );

    file.close();
}
//基础设置-邮件测试按钮
void setupWidget::on_pushButton_7_clicked()
{
    QMessageBox::critical(this,"发送测试","无法连接到目标邮箱",QMessageBox::Ok);
}
//listWidget页面切换
void setupWidget::on_listWidget_currentRowChanged(int currentRow)
{
    ui->stackedWidget->setCurrentIndex(currentRow);
}


//显示数据库数据
void setupWidget::initTable()
{
    //清空表格数据内容
    ui->tableWidget->clear();
    //重新设定表格行数为0
    ui->tableWidget->setRowCount(0);

    //记录sql语句
    QSqlQuery q("select * from info");
    //接收语句执行返回值
    QSqlRecord res = q.record();
    int cols = res.count();//获取列数
    //设置表格列数
    ui->tableWidget->setColumnCount(cols);
    //定义表头列表
    QStringList headerlist;
    //输入表头
    for(int i=0;i<cols;i++){
        headerlist.append(res.fieldName(i));
    }
    ui->tableWidget->setHorizontalHeaderLabels(headerlist);

    //循环给每行赋值
    while(q.next()){
        QString item = q.value(0).toString();
        QString name = q.value(1).toString();
        QString location = q.value(2).toString();
        QString ip = q.value(3).toString();
        QString remark = q.value(4).toString();
        qDebug()<<item<<","<<name<<","<<location<<","<<ip<<","<<remark;
        //向表格尾部插入空行
        int rows = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(rows);

        //为本行添加内容
        //ui->tableWidget->setItem(rows,0,new QTableWidgetItem(item));//监测项
        ui->tableWidget->setItem(rows,1,new QTableWidgetItem(name));
        //ui->tableWidget->setItem(rows,2,new QTableWidgetItem(location));//监测地址
        ui->tableWidget->setItem(rows,3,new QTableWidgetItem(ip));
        ui->tableWidget->setItem(rows,4,new QTableWidgetItem(remark));


        QComboBox *itemBox = new QComboBox;//创建下拉列表
        QStringList list;
        list<<"PM2.5"<<"PM10"<<"噪音"<<"AQI"<<"气温"<<"湿度"<<"风速"<<"污水池PH值<<用电量<<用水量";
        itemBox->addItems(list);//添加进组合框
        ui->tableWidget->setCellWidget(rows,0,itemBox);//将框子放进表格里

        QComboBox *locationBox = new QComboBox;//创建下拉列表
        QStringList list2;
        list2<<"生活区"<<"办公区"<<"施工区";
        locationBox->addItems(list2);//添加进组合框
        ui->tableWidget->setCellWidget(rows,0,locationBox);//将框子放进表格里

    }
}

//增加按钮
void setupWidget::on_addbtn_clicked()
{

    qDebug()<<"进入槽函数";
    addDevice* dlg=new addDevice;//创建输入窗口
    dlg->setAttribute(Qt::WA_DeleteOnClose);//设置窗口属性为自动删除
    dlg->show();
    //如果接收到自定义信号就触发
    connect(dlg,&addDevice::sendDeviceInfo,[=](QString item,QString name,QString location,QString ip,QString remark){
        //实例化对象
        QSqlQuery query;
        //准备sql语句
        QString sql=QString("INSERT INTO deviceType(\"检测项\",\"设备名称\",\"监测地址\",\"设备IP\",\"备注\") VALUES ('%1','%2','%3','%4','%5')").arg(item).arg(name).arg(location).arg(ip).arg(remark);
        //执行sql语句
        if(!query.exec(sql)){
            // 处理执行失败的情况
            qDebug() << "Error executing query:" << query.lastError().text();
        }
    });

    //更新表格
    initTable();

}


//删除按钮 槽
void setupWidget::on_pushButton_10_clicked()
{
    QSqlQuery query;
    //query.prepare("DELETE FROM \"deviceType\" WHERE (\"ID\"=?)");
    //query.bindValue(0, ui->tableWidget->item(ui->tableWidget->currentRow(),0)->text());
    QString sql=QString("DELETE FROM \"main\".\"deviceType\" WHERE (\"ID\"=%1)").arg(ui->tableWidget->item(ui->tableWidget->currentRow(),0)->text());

    if(!query.exec(sql)){
        qDebug() << "Error executing query:" <<query.lastError().text();
    }

    initTable();
}


